package tech.aistar.day11;

/**
 * 本类用来演示:
 * 写俩个线程,一个线程负责打印1~52,另外一个线程负责打印A-Z,打印的顺序必须是12A34B56C ....   5152Z
 *
 * @author: success
 * @date: 2022/8/2 9:14 上午
 */
public class LetterAndNumDemo {

    //俩个线程 - boolean
    private boolean flag = false;

    public void printNum(){
        synchronized (this) {
            for (int i = 1; i <= 52; i += 2) {
                if(flag){
                    try {
                        wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.print(i);
                System.out.print(i + 1);
                flag = true;
                notify();
            }
        }
    }

    public void printLetter(){
        synchronized (this){
            for (int i = 65; i <=91; i++) {
                if(!flag){
                    try {
                        wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.print((char)i);
                flag = false;
                notify();
            }
        }
    }
}
class LetterTest{
    public static void main(String[] args) {
        LetterAndNumDemo demo = new LetterAndNumDemo();
        Thread num = new Thread(()->demo.printNum());
        Thread letter = new Thread(()->demo.printLetter());
        num.start();
        letter.start();
    }
}
